package com.lg.cms.controller.admin.article;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.lg.cms.entity.Category;
import com.lg.cms.service.CategoryService;
import com.lg.cms.utils.SubjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 栏目管理
 * @Author: wuyuhang
 * @create: 2020-10-07 18:07
 */
//@RestController
@Controller
@RequestMapping("/admin/category")
@Api(tags = "栏目管理")
public class WYHCategoryController {

    @Autowired
    CategoryService categoryService;

    /**
     * 栏目页面
     * @return
     */
    @GetMapping("/list")
    public String list(Model model, HttpServletRequest request){
        model.addAttribute("user", SubjectUtil.getSuperUser());
        model.addAttribute("request", request);
        model.addAttribute("categoryList", categoryService.list(new QueryWrapper<Category>().orderByAsc("sort")));
        return "/admin/wyh-test/article/category_list";
    }

    /**
     * 新增栏目
     * @param category
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation("新增栏目")
    public R<?> add(Category category){
        if(!category.checkData()){
            return R.failed("请输入必填数据");
        }
        Category c = categoryService.getOne(
                new QueryWrapper<Category>().eq("alias", category.getAlias()).or().eq("name", category.getName()));
        if(c != null){
            return R.failed("栏目名称或别名已存在");
        }
        boolean result = categoryService.save(category);
        if(!result){
            return R.failed("新增栏目失败");
        }
        return R.ok(category);
    }

    /**
     * 获取栏目
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ResponseBody
    @ApiOperation("根据id获取栏目信息")
    public R<?> getById(@PathVariable("id") Integer id){
        Category category = categoryService.getById(id);
        if(category == null){
            return R.failed("栏目不存在");
        }
        return R.ok(category);
    }


    /**
     * 修改,更新栏目信息
     * @param category
     * @return
     */
    @PutMapping("/update")
    @ResponseBody
    @ApiOperation("修改栏目")
    public R<?> update(Category category){
        if(!category.checkData()){
            return R.failed("请输入必填信息");
        }
        if(category.getId() == null){
            return R.failed("请选择栏目");
        }
        Category oldCategory = categoryService.getById(category.getId());
        if(oldCategory == null){
            return R.failed("栏目不存在");
        }
        oldCategory.setAlias(category.getAlias());
        oldCategory.setDescription(category.getDescription());
        oldCategory.setName(category.getName());
        oldCategory.setSort(category.getSort());
        oldCategory.setParentId(category.getParentId());
        boolean result = categoryService.updateById(oldCategory);
        if(!result){
            return R.failed("修改失败");
        }
        return R.ok(category);
    }

    /**
     * 删除栏目
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    @ApiOperation("删除栏目")
    public R<?> delete(Integer id){
        if(id == null){
            return R.failed("删除的栏目不能为空");
        }
        Category category = categoryService.getById(id);
        if(category.getName().equals("视频") || category.getName().equals("图片")
                || category.getAlias().equals("img") || category.getAlias().equals("video")){
            return R.failed("视频, 图片栏目不能删除");
        }
        boolean result = categoryService.removeById(id);
        if(!result){
            return R.failed("删除失败");
        }
        return R.ok(null);
    }
}